<?php

namespace App\Model;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ComeBack extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'ddyy_come_back_config';//回归活动配置
    protected $siginTable = 'ddyy_activity_sigin_config';//活动配置签到表
    protected $dayTaskTable = 'ddyy_day_task_config';//活动每日任务

    //获取配置列表
    public function getComeBackConfigList($page=1, $limit=10, $name, $start_time, $end_time){
        $where = function($qurey)use($name,$start_time, $end_time) {
            $qurey->where('is_delete',0);
            if(!empty($name)){
                $qurey->where('ac_name','like','%'.$name);
            }
            if(!empty($start_time) && !empty($end_time)){
                $start_time = Carbon::parse($start_time)->startOfDay()->toDateTimeString();
                $end_time = Carbon::parse($end_time)->endOfDay()->toDateTimeString();
                $qurey->where('created_at','>=',$start_time);
                $qurey->where('created_at','<=',$end_time);
            }
        };
        $data = DB::connection($this->connection)->table($this->table)->where($where)->paginate($limit,['*'],'page',$page);
        return $data;
    }

    //添加配置
    public function addComeBackConfig($name, $icon, $start_time, $end_time, $type, $excelUrl, $days){
        $data = [
            'name' => $name,
            'icon' => $icon,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'type' => $type,
            'days' => $days,
        ];
        $finalstr = [];
        //获取Excel内容;
        $fileurl = build_image( '/'.$excelUrl);
        //下载文件
        $filepath = storage_path().'/'.'comeback.xls';
        file_put_contents($filepath, file_get_contents($fileurl));
        //读取Excel信息
        $array = excel_to_array($filepath, 1, '', 1);
        if (!empty($array['content'])) {
            foreach ($array['content'] as $k => $v) {
                    $v=array_filter($v);
                    $phoneArr[] = $v;      
            }
            $phoneArr = array_reduce($phoneArr,'array_merge',[]);
            $finalstr = implode(',', $phoneArr);
        }
        $data['ids'] = $finalstr;
        $data['execl_url'] = $excelUrl;
        $res = curl_request(getKkaudioApiUrl() . '/inner/ddyy/add_mission_config', $data, 'urlencoded', 'POST');
        return $res;
    }

    //编辑配置
    public function editComeBackConfig($id, $name, $icon, $start_time, $end_time, $type, $excelUrl, $days){
        $data = [
            'id' => $id,
            'name' => $name,
            'icon' => $icon,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'type' => $type,
            'days' => $days,
        ];
        $finalstr = [];
        //获取Excel内容;
        $fileurl = build_image( '/'.$excelUrl);
        //下载文件
        $filepath = storage_path().'/'.'comeback.xls';
        file_put_contents($filepath, file_get_contents($fileurl));
        //读取Excel信息
        $array = excel_to_array($filepath, 1, '', 1);
        if (!empty($array['content'])) {
            foreach ($array['content'] as $k => $v) {
                    $v=array_filter($v);
                    $phoneArr[] = $v;      
            }
            $phoneArr = array_reduce($phoneArr,'array_merge',[]);
            $finalstr = implode(',', $phoneArr);
        }
        $data['ids'] = $finalstr;
        $data['execl_url'] = $excelUrl;
        $res = curl_request(getKkaudioApiUrl() . '/inner/ddyy/edit_mission_config', $data, 'urlencoded', 'POST');
        return $res;
    }

    //删除活动
    public function delComeBackConfig($id){
        $data['id'] = $id;
        $res = curl_request(getKkaudioApiUrl() . '/inner/ddyy/del_mission_config', $data, 'urlencoded', 'POST');
        return $res;
    }

    //获取对应配置下签到配置列表
    public function getSiginList($id, $page, $limit){
        $res = DB::connection($this->connection)->table($this->siginTable)->where('act_id',$id)->where('is_delete',0)->paginate($limit,['*'],'page',$page);
        return $res;
    }

    //新增签到配置
    public function addSiginConfig($id, $day, $send_type, $goods_id, $send_num, $img, $award_name){
        $data = [
            'act_id' => $id,
            'day'=> $day,
            'send_type'=> $send_type,
            'send_num' => $send_num,
            'goods_id' => $goods_id,
            'img' => $img,
            'award_name' => $award_name
        ];
        $res = curl_request(getKkaudioApiUrl() . '/inner/ddyy/add_sigin_config', $data, 'urlencoded', 'POST');
        return $res;
    }

    //编辑签到配置
    public function editSiginConfig($sigin_id,$day, $send_type, $goods_id, $send_num, $img, $award_name){
        $data = [
            'sigin_id' => $sigin_id,
            'day'=> $day,
            'send_type'=> $send_type,
            'send_num' => $send_num,
            'goods_id' => $goods_id,
            'img' => $img,
            'award_name' => $award_name
        ];
        $res = curl_request(getKkaudioApiUrl() . '/inner/ddyy/edit_sigin_config', $data, 'urlencoded', 'POST');
        return $res;
    }

    //删除签到配置
    public function delSiginConfig($sigin_id){
        $data['sigin_id'] = $sigin_id;
        $res = curl_request(getKkaudioApiUrl() . '/inner/ddyy/del_sigin_config', $data, 'urlencoded', 'POST');
        return $res;
    }

    //获取每日任务列表
    public function getTaskList($id, $page, $limit, $is_day){
        $data = DB::connection($this->connection)->table($this->dayTaskTable)->where('act_id',$id)->where('is_day',$is_day)->where('is_delete',0)->paginate($limit,['*'],'page',$page);
        return $data;
    } 
    
    //添加每日任务
    public function addTask($id,  $desc, $type, $need_value, $award_type, $award_name, $award_num, $award_id, $is_day){
        $data = [
            'act_id' => $id,
            'desc' => $desc,
            'type' => $type,
            'need_value'=>$need_value,
            'award_type'=>$award_type,
            'award_name'=>$award_name,
            'award_num'=>$award_num,
            'award_id'=> $award_id,
            'is_day' => $is_day
        ];
        $res = curl_request(getKkaudioApiUrl() . '/inner/ddyy/add_day_task', $data, 'urlencoded', 'POST');
        return $res;
    }

    //编辑每日任务
    public function editTask($id,  $desc, $type, $need_value, $award_type, $award_name, $award_num, $award_id, $is_day){
        $data = [
            'task_id' => $id,
            'desc' => $desc,
            'type' => $type,
            'need_value'=>$need_value,
            'award_type'=>$award_type,
            'award_name'=>$award_name,
            'award_num'=>$award_num,
            'award_id'=> $award_id,
            'is_day' => $is_day,
        ];
        $res = curl_request(getKkaudioApiUrl() . '/inner/ddyy/edit_day_task', $data, 'urlencoded', 'POST');
        return $res;
    }

    //删除每日任务配置
    public function delTaskConfig($id){
        $data['task_id'] = $id;
        $res = curl_request(getKkaudioApiUrl() . '/inner/ddyy/del_task_config', $data, 'urlencoded', 'POST');
        return $res;
    }
}